﻿using CrankShaftAnalysisSystem.SubForm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CrankShaftAnalysisSystem
{
    public partial class FormComplete : Form
    {
        public event Action OnComplete;
        private bool DataSaved = false;
        public FormResult formResult;
        public FormComplete()
        {
            InitializeComponent();
            formResult = new FormResult();
        }

        private void FormComplete_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }

        public DataStorage DataBase { get; set; }
        public void SaveFile()
        {
            if(DataBase != null)
            {
                DataBase.SaveResults();
                DataBase = null;
            }
        }

        private void btnSaveFile_Click(object sender, EventArgs e)
        {
            Sunny.UI.UIMessageTip.Show("测试完成");
            DataSaved = false;
            if (OnComplete != null)
                OnComplete();
        }

        private void FormComplete_Load(object sender, EventArgs e)
        {
            tmLoop.Start();
            UIHelper.SetPanelToCenter(ref mainPanel);
        }

        private void tmLoop_Tick(object sender, EventArgs e)
        {
            ledManulOn.On = Tools.GetBitState(PLCMaster.Instance.Device.Info.ButtonInfo, VirtualDevice.Mask.MANUALON);
            ledPartRelease.On = PLCMaster.Instance.PartCanBePlaced;
            btnCalibrate.Enabled = checkAdvanced.Checked;
            //btnFinish.Enabled = ledManulOn.On && ledPartRelease.On;
            btnFinish.Enabled = DataSaved;
            if(DataBase != null)
                lbResultInfo.Text = $"{DataStorage.uuid}, 数据数量：{DataBase.Clouds.Count}";
        }

        private void btnDealResult_Click(object sender, EventArgs e)
        {
            SaveFile();
            var handler = new CrankShaftResultHandler.ResultHandler();
            handler.LoadRefrenceAndConfig(MeasureSystem.MeasureSetting.RefrenceReport);
            DataSaved = true;

            var ret = handler.ReadCSV(DataStorage.GetCsvPath());
            //var ret = handler.ReadCSV(@"C:\Projects\CrankShaftAnalysisSystem\bin\Debug\Results\RV20E137-03291.csv");
            if (ret is null)
            {
                Sunny.UI.UIMessageBox.Show("没有找到数据文件", "ERROR", Sunny.UI.UIStyle.Red);
                return;
            }
            handler.Deal();
            handler.SaveResult(DataStorage.uuid);
            txtLog.Text = handler.ToString().Replace(",", "\r\n");
            formResult.RefreshList(handler.GetTitle().Split(','), handler.ToString().Split(','), handler.IsValueOk);
            formResult.Show();
            formResult.BringToFront();
        }

        private void btnCalibrate_Click(object sender, EventArgs e)
        {
            var ret = MeasureSystem.Instance.CalibrateSystem(DataStorage.GetCsvPath());
            if (ret)
                Sunny.UI.UIMessageBox.Show("自标定成功");
            else
                Sunny.UI.UIMessageBox.Show("自标定失败");
        }
    }
}
